#ifndef _XMLREAD_H_
#define _XMLREAD_H_

#include <fstream>
#include <iostream>
#include <string>
#include <sstream>
#include "../wtpack2contafilling/tinyxml/tinyxml.h"
#include "Box.h"

using namespace std;

int escribirPruebaXML(string);
int escribirEntradaXML(const vector<vector<Box> > &);


//Tags del XML
#define	ROOT_TAG		"root"
#define WIDTH_TAG		"width"
#define HEIGHT_TAG		"height"
#define DEPTH_TAG		"depth"
#define BOXES_TAG		"boxes"
#define BOX_TAG			"box"
#define DESTINATION_TAG	"destination"
#define CLIENT_TAG		"client"
#define TYPE_TAG		"type"
#define ID_TAG			"id"
#define WEIGHT_TAG		"weight"
#define FILE_TAG		"file"
#define INPUT_TAG		"input"
#define OUTPUT_TAG		"output"
#define CONTAINER_TAG	"container"
#define GENERATIONS_TAG	"generations"
#define MAX_TAG			"max"
#define POPULATION_TAG	"population"
#define PROBABILITY_TAG	"probability"
#define CROSSOVER_TAG	"crossover"
#define MUTATION_TAG	"mutation"
#define CROSS_OP_TAG	"crossover_op"
#define MUTAT_OP_TAG	"mutation_op"
#define FIT_OP_TAG		"fitness"
#define CONVERGENCE_TAG	"convergence"
#define WEIGHTS_TAG		"weights"
#define W_CROSS_TAG		"cross"
#define W_WEIGHT_TAG	"block_diff"
#define	X_TAG			"x"
#define Y_TAG			"y"
#define Z_TAG			"z"
#define CONTAINERID_TAG	"container_id"
#define NUM_TAG			"num"
#define WEIGHT_ON_TAG	"weight_on"
#define PERCENTIL_TAG	"percentiles"
#define VOLUME_TAG		"volume"
#define AREA_TAG		"area"


string DobToStr(double n) {
	stringstream result;
	result << n;
	return result.str();
}



void escribirEntradaXML(string outfile, vector<Box> &entrada)
{
	
		TiXmlDocument doc;  
		TiXmlElement* msg,
					* caja;
 		TiXmlDeclaration* decl = new TiXmlDeclaration( "1.0", "UTF-8", "yes" );  
		doc.LinkEndChild( decl );  
	 
		TiXmlElement * root = new TiXmlElement( ROOT_TAG );  
		doc.LinkEndChild( root ); 

		string destination, client, id, type, width, height, depth, weight;
		

		TiXmlElement * cajas = new TiXmlElement( BOXES_TAG);  
		root->LinkEndChild( cajas );

		//Escribir los datos de las cajas 1 a 1
		for(int i=0; i<entrada.size(); i++)
		{
			destination = entrada[i].getDestination();
			client = entrada[i].getClient();
			type = entrada[i].getName();
			id = DobToStr(entrada[i].getID());

			width = DobToStr(entrada[i].getWidth());
			height = DobToStr(entrada[i].getHeight());
			depth = DobToStr(entrada[i].getDepth());
			weight = DobToStr(entrada[i].getWeight());

			caja = new TiXmlElement ( BOX_TAG );

			msg = new TiXmlElement( DESTINATION_TAG );		
			msg->LinkEndChild( new TiXmlText( destination.c_str() ));  
			caja->LinkEndChild( msg );

			msg = new TiXmlElement( CLIENT_TAG );		
			msg->LinkEndChild( new TiXmlText( client.c_str() ));  
			caja->LinkEndChild( msg );

			msg = new TiXmlElement( ID_TAG );		
			msg->LinkEndChild( new TiXmlText( id.c_str() ));  
			caja->LinkEndChild( msg );

			msg = new TiXmlElement( TYPE_TAG );		
			msg->LinkEndChild( new TiXmlText( type.c_str() ));  
			caja->LinkEndChild( msg );

			msg = new TiXmlElement( WIDTH_TAG );		
			msg->LinkEndChild( new TiXmlText( width.c_str() ));  
			caja->LinkEndChild( msg );

			msg = new TiXmlElement( HEIGHT_TAG );		
			msg->LinkEndChild( new TiXmlText( height.c_str() ));  
			caja->LinkEndChild( msg );

			msg = new TiXmlElement( DEPTH_TAG );		
			msg->LinkEndChild( new TiXmlText( depth.c_str() ));  
			caja->LinkEndChild( msg );

			msg = new TiXmlElement( WEIGHT_TAG );		
			msg->LinkEndChild( new TiXmlText( weight.c_str() ));  
			caja->LinkEndChild( msg );

			cajas->LinkEndChild( caja );
		}
	
		string salida = outfile + ".xml";
		doc.SaveFile( salida.c_str() );

}

#endif